package com.neuedu.charge.cost.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.charge.base.entity.Cost;
import com.neuedu.charge.base.entity.User;
import com.neuedu.charge.base.response.Result;
import com.neuedu.charge.cost.fegin.UserFeign;
import com.neuedu.charge.cost.mapper.CostMapper;
import com.neuedu.charge.cost.service.CostService;
import com.neuedu.charge.cost.vo.CostVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<CostMapper, Cost> implements CostService {
    @Autowired
    private UserFeign userFeign;

    @Override
    public List<CostVO> getCostList() {
        List<Cost> list = this.list();
        List<CostVO> voList=new ArrayList<>();
        for (Cost cost : list) {
            CostVO vo = new CostVO();
            // 根据用户id查找用户数据
            Result result = userFeign.getUserById(cost.getUserId());
            LinkedHashMap user=(LinkedHashMap)result.getData();
            if (null != user) {
                vo.setUsername(user.get("username").toString());
                vo.setRealName(user.get("realname").toString());
                vo.setId(cost.getId());
                vo.setPrice(cost.getPrice());
                vo.setDegree(cost.getDegree());
                vo.setTotalPrice(cost.getPrice().multiply(cost.getDegree()));
            }
            voList.add(vo);
        }
        return voList;
    }
}
